/**
 * 
 */
package com.jdy.haoduoaiteacher.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import com.jdy.haoduoaiteacher.util.Tools;
import com.ycsj.goldmedalnewconcept.R;

/**
 * @author zp 2016-4-15 上午10:01:01
 */
public class HorizontalIndicator extends View {
  protected int WIDTH;

  protected int HEIGHT;

  private float backgroundLength = 80;

  private float backgroundHeight = 10;

  private int backgroundColor = Color.argb(255, 232, 232, 232);

  private int indicatorColor = Color.argb(255, 249, 176, 48);

  private final int smoothGapLength = 2;

  private float ovalLength = backgroundHeight;// act as inner part of
  // backgroundLength

  private float indicatorLengh = backgroundLength / 2 + ovalLength / 2;

  private int lp;

  private int tp;

  private int rp;

  private int bp;

  private final int MIN_OFFSET = 0;

  private float MAX_OFFSET = backgroundLength / 2 - ovalLength / 2;

  private final Handler mHandler = new Handler();

  private int currentOffset;

  private final Runnable updateOffsetRunnable;

  private boolean forward = true;

  private final Paint mPaint;

  /**
   * @param context
   */
  public HorizontalIndicator(Context context) {
    this(context, null);
    // TODO Auto-generated constructor stub
  }

  /**
   * @param context
   * @param attrs
   */
  public HorizontalIndicator(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
    // TODO Auto-generated constructor stub
  }

  /**
   * @param context
   * @param attrs
   * @param defStyle
   */
  public HorizontalIndicator(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub

    final TypedArray typedArray = context.obtainStyledAttributes(attrs,
        R.styleable.HorizontalIndicator);

    backgroundColor = typedArray.getColor(
        R.styleable.HorizontalIndicator_hi_background_color,
        Color.argb(255, 232, 232, 232));

    indicatorColor = typedArray.getColor(
        R.styleable.HorizontalIndicator_hi_indicator_color,
        Color.argb(255, 249, 176, 48));

    float ratio = Tools.getPixelRatio(context);

    backgroundLength = typedArray.getInt(
        R.styleable.HorizontalIndicator_hi_length, (int) backgroundLength)
        * ratio;

    backgroundHeight = typedArray.getInt(
        R.styleable.HorizontalIndicator_hi_height, (int) backgroundHeight)
        * ratio;

    ovalLength = backgroundHeight;

    indicatorLengh = backgroundLength / 2 + ovalLength / 2;

    MAX_OFFSET = backgroundLength / 2 - ovalLength / 2;

    typedArray.recycle();

    currentOffset = 0;

    mPaint = new Paint();

    mPaint.setAntiAlias(true);

    updateOffsetRunnable = new Runnable() {
      @Override
      public void run() {
        try {
          if (currentOffset > MAX_OFFSET) {
            forward = false;
          } else if (currentOffset < MIN_OFFSET) {
            forward = true;
          }
          if (forward) {
            currentOffset++;
          } else {
            currentOffset--;
          }
          invalidate();
        } finally {
          // 100% guarantee that this always happens, even if
          // your update method throws an exception
          mHandler.postDelayed(updateOffsetRunnable, 30);
        }
      }
    };

    // updateOffsetRunnable.run();

  }

  public void onScrollChanged(int externalCurrentProgress, int externalMax) {
    // if (externalMax - 5 < 0) {
    // this.setVisibility(View.GONE);
    // return;
    // }
    if (externalMax != 0) {
      currentOffset = (int) (MAX_OFFSET * externalCurrentProgress / externalMax);
      invalidate();
    }
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldW, int oldH) {
    WIDTH = w;
    HEIGHT = h;
    getLeftPaddingOffset();
    getTopPaddingOffset();
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int desiredWidth = (int) backgroundLength;
    int desiredHeight = (int) backgroundHeight;

    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    int width;
    int height;

    // Measure Width
    if (widthMode == MeasureSpec.EXACTLY) {
      // Must be this size
      width = widthSize;
    } else if (widthMode == MeasureSpec.AT_MOST) {
      // Can't be bigger than...
      width = Math.min(desiredWidth, widthSize);
    } else {
      // Be whatever you want
      width = desiredWidth;
    }

    // Measure Height
    if (heightMode == MeasureSpec.EXACTLY) {
      // Must be this size
      height = heightSize;
    } else if (heightMode == MeasureSpec.AT_MOST) {
      // Can't be bigger than...
      height = Math.min(desiredHeight, heightSize);
    } else {
      // Be whatever you want
      height = desiredHeight;
    }

    // MUST CALL THIS
    setMeasuredDimension(width, height);
  }

  @Override
  protected void onDraw(Canvas canvas) {

    drawIndicatorBackground(canvas);
    drawIndicator(canvas);
    // invalidate();
  }

  private void drawIndicatorBackground(Canvas canvas) {
    drawShape(canvas, backgroundColor, backgroundLength, 0);
  }

  private void drawIndicator(Canvas canvas) {
    drawShape(canvas, indicatorColor, indicatorLengh, currentOffset);
  }

  private void drawShape(Canvas canvas, int color, float length, float offset) {

    mPaint.setColor(color);

    RectF leftOval = new RectF(lp + offset, tp, offset + lp + ovalLength,
        backgroundHeight);

    canvas.drawOval(leftOval, mPaint);

    RectF centerRect = new RectF(
        offset + lp + ovalLength / 2 + smoothGapLength, tp, offset + lp
            + length - ovalLength / 2 - smoothGapLength, backgroundHeight);

    canvas.drawRect(centerRect, mPaint);

    RectF rightOval = new RectF(offset + length + lp - ovalLength, tp, offset
        + length + lp, backgroundHeight);

    canvas.drawOval(rightOval, mPaint);
  }

}
